package com.example.controller;

import com.example.Repository.TagRepository;
import com.example.Repository.TypeRepository;
import com.example.domain.Blog;
import com.example.domain.property.Tag;
import com.example.domain.property.Type;
import com.example.domain.property.User;
import com.example.service.BlogService;
import com.example.service.TagService;
import com.example.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeRepository typeRepository;
    @Autowired
    private TagRepository tagRepository;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;
    //分页查询
    @PostMapping("/list/{currentPage}/{PageSize}")
    @ResponseBody
    public Page<Blog> index1(@PathVariable(value = "currentPage")int currentPage,@PathVariable(value = "PageSize") int PageSize,
                              @RequestBody Blog blog){
        Type type=new Type();
        type.setId(blog.getTypeId());  //把typeId的id给type
        blog.setType(type);
        Pageable pageable= PageRequest.of(currentPage,PageSize);
        Page<Blog> page = blogService.selectBlogPage(pageable, blog);
        return page;
    }
    //推荐文章分页查询
    @PostMapping("/blogPage/{currentPage}/{PageSize}")
    @ResponseBody
    public Page<Blog> index11(@PathVariable(value = "currentPage")int currentPage,@PathVariable(value = "PageSize") int PageSize,
                             @RequestBody Blog blog){
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "updateTime");  //按时间降序排序
        Sort sort = Sort.by(order);
        Pageable pageable= PageRequest.of(currentPage,PageSize,sort);
        Page<Blog> page = blogService.gainBlogPage(pageable, blog);
        return page;
    }
    //获取下拉列表和行修改所需数据
    @GetMapping("/gainTypeTag")
    @ResponseBody
    public Map<String,Object> index9(HttpSession session){
        List<Type> typeList = typeRepository.findAll();
        List<Tag> tagList = tagRepository.findAll();
        Map<String,Object> map=new HashMap<>();
        map.put("typeList",typeList);
        map.put("tagList",tagList);
        return map;
    }
    //新增数据和修改数据
    @PostMapping("/insert")
    public String index2(Blog blog, HttpSession session){
        blog.setUser((User)session.getAttribute("user"));
        blog.setType((Type) typeService.selectOne(blog.getType().getId()));
        blog.setTagList(tagService.listTag(blog.getTagIds()));
        boolean flag =blogService.insert(blog);
        if(flag){
            session.setAttribute("blogMeg","正确");
        }else{
            session.setAttribute("blogMeg","错误");
        }
        return "redirect:/back/host/manage.html";
    }

    //后台管理页查看添加是否成功
    @GetMapping("/success")
    @ResponseBody
    public String index6(HttpSession session){
        String str= (String)session.getAttribute("blogMeg");
        session.removeAttribute("blogMeg");
        return str;
    }
    //获取修改页下拉列表所需数据
    @GetMapping("/gainUpdateTypeTag")
    @ResponseBody
    public Map<String,Object> index7(HttpSession session){
        List<Type> typeList = typeRepository.findAll();
        List<Tag> tagList = tagRepository.findAll();

        Long id = (Long)session.getAttribute("UpdateBlogId");
        Blog blog = blogService.selectOneId(id);

        Map<String,Object> map=new HashMap<>();
        map.put("typeList",typeList);
        map.put("tagList",tagList);
        map.put("upDateType",blog.getType());
        map.put("upDateTagList",blog.getTagList());
        return map;
    }
    //获取要更新的行数据
    @GetMapping("/gainBlog")
    @ResponseBody
    public Blog index10(HttpSession session){
        Long id = (Long)session.getAttribute("UpdateBlogId");
        Blog blog = blogService.selectOneId(id);
        System.out.println("数据库数据："+blog);
        return blog;
    }
    //查询id
    @GetMapping("/selectId/{id}")
    @ResponseBody
    public boolean index3(@PathVariable(value = "id") Long id,HttpSession session){
        session.setAttribute("UpdateBlogId",id);
        return true;
    }
    //根据id更新数据
    @PutMapping("/updateRow")
    @ResponseBody
    public Boolean index4(@RequestBody Blog blog,HttpSession session){
        session.removeAttribute("updateId");
        boolean flag = false;
        return flag;
    }
    //根据id删除数据
    @DeleteMapping("/deleteRow/{id}")
    @ResponseBody
    public Boolean index5(@PathVariable(value = "id") Long id){
        boolean flag = blogService.deleteById(id);
        return flag;
    }
    //归档页时间查询
    @GetMapping("/gainTimeBlog")
    @ResponseBody
    public List<Blog> index12(){
        List<Blog> list = blogService.findAllByCreateTimeLike();
        return list;
    }
    //标签页标签文章查询
    @PostMapping("/list/{currentPage}/{PageSize}/{id}")
    @ResponseBody
    public Page<Blog> index13(@PathVariable(value = "currentPage")int currentPage,@PathVariable(value = "PageSize") int PageSize,
                              @PathVariable(value = "id")Long id){
        Tag tag=new Tag();tag.setId(id);
        List<Tag> list=new ArrayList<>();list.add(tag);
        Blog blog=new Blog();
        blog.setTagList(list);
        Pageable pageable= PageRequest.of(currentPage,PageSize);
        Page<Blog> page = blogService.selectBlogPage(pageable, blog);
        return page;
    }
}
